package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@Slf4j
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    @Autowired
    private VodService vodService;

    /**
     * 批量获取下单付费声音列表
     * @param trackId
     * @param trackCount
     * @return
     */
    @Operation(summary = "批量获取下单付费声音列表")
    @GetMapping("findPaidTrackInfoList/{trackId}/{trackCount}")
    public Result findPaidTrackInfoList(@PathVariable Long trackId,
                                        @PathVariable Integer trackCount){
        //  调用服务层方法.
        List<TrackInfo> trackInfoList = this.trackInfoService.findPaidTrackInfoList(trackId,trackCount);
        //  返回数据
        return Result.ok(trackInfoList);
    }


    /**
     * 获取用户声音分集购买支付列表
     * @param trackId
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "获取用户声音分集购买支付列表")
    @GetMapping("/findUserTrackPaidList/{trackId}")
    public Result findUsertrackIdNoReaptList(@PathVariable Long trackId){
        //  获取用户Id
        Long userId = AuthContextHolder.getUserId();
        //  调用服务层方法
        List<Map<String,Object>> mapList = this.trackInfoService.findUsertrackIdNoReaptList(userId,trackId);
        //  返回数据
        return Result.ok(mapList);
    }

    //  http://127.0.0.1/api/album/trackInfo/findAlbumTrackPage/125/1/10

    /**
     * 查看专辑对应的声音列表
     * @param albumId
     * @param page
     * @param limit
     * @return
     */
    @GuiGuLogin(required = false)
    @Operation(summary = "专辑对应的声音列表")
    @GetMapping("/findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result findAlbumTrackPage(@PathVariable Long albumId,
                                     @PathVariable Long page,
                                     @PathVariable Long limit){
        //  获取用户Id
        Long userId = AuthContextHolder.getUserId();
        //  将查询数据封装到page中.
        Page<AlbumTrackListVo> listVoPage = new Page<>(page, limit);
        //  调用服务层方法。
        IPage<AlbumTrackListVo> albumTrackListVoIPage = trackInfoService.findAlbumTrackPage(albumId,listVoPage,userId);
        //  返回数据
        return Result.ok(albumTrackListVoIPage);
    }
    /**
     * 修改声音
     * @param trackId
     * @param trackInfoVo
     * @return
     */
    @Operation(summary = "修改声音")
    @PutMapping("/updateTrackInfo/{trackId}")
    public Result updateTrackInfo(@PathVariable Long trackId, @RequestBody TrackInfoVo trackInfoVo) {
        //	调用服务层方法.
        this.trackInfoService.updateTrackInfo(trackId, trackInfoVo);
        //	返回数据
        return Result.ok();

    }

    /**
     * 根据声音Id获取信息
     *
     * @param trackId
     * @return
     */
    @GetMapping("/getTrackInfo/{trackId}")
    public Result getTrackInfo(@PathVariable Long trackId) {
        //	调用服务层方法.
        TrackInfo trackInfo = trackInfoService.getTrackInfo(trackId);
        //	返回数据
        return Result.ok(trackInfo);
    }

    /**
     * 根据声音Id删除声音
     *
     * @param trackId
     * @return
     */
    @Operation(summary = "根据声音Id删除声音")
    @DeleteMapping("/removeTrackInfo/{trackId}")
    public Result removeTrackInfo(@PathVariable Long trackId) {
        //	调用服务层方法
        trackInfoService.removeTrackInfo(trackId);
        //	返回数据
        return Result.ok();
    }

    //	http://127.0.0.1/api/album/trackInfo/findUserTrackPage/1/10

    /**
     * 查看声音列表
     *
     * @param page
     * @param limit
     * @param trackInfoQuery
     * @return
     */
    @GuiGuLogin // 默认值是true;必须要登录；
    @Operation(summary = "查看声音列表")
    @PostMapping("/findUserTrackPage/{page}/{limit}")
    public Result findUserTrackPage(@PathVariable Long page,
                                    @PathVariable Long limit,
                                    @RequestBody TrackInfoQuery trackInfoQuery) {
        //	获取用户Id
        //	userId = 1; 会将用户Id 存储到本地中.
        Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
        trackInfoQuery.setUserId(userId);
        //	创建一个Page 对象；
        Page<TrackListVo> trackListVoPage = new Page<>(page, limit);
        //	调用服务层方法.
        IPage<TrackListVo> listVoIPage = this.trackInfoService.findUserTrackPage(trackListVoPage, trackInfoQuery);
        //	返回数据
        return Result.ok(listVoIPage);
    }

    //	http://127.0.0.1/api/album/trackInfo/saveTrackInfo

    /**
     * 保存声音
     *
     * @param trackInfoVo
     * @return
     */
    @Operation(summary = "保存声音")
    @PostMapping("saveTrackInfo")
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        //	调用服务层方法.
        Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
        trackInfoService.saveTrackInfo(trackInfoVo, userId);
        //	返回
        return Result.ok();
    }

    //	http://127.0.0.1/api/album/trackInfo/uploadTrack

    /**
     * 上传声音
     *
     * @param file
     * @return
     */
    @Operation(summary = "上传声音")
    @PostMapping("/uploadTrack")
    public Result uploadTrack(MultipartFile file) {
        //	调用服务层方法
        Map<String, Object> map = vodService.uploadTrack(file);
        //	返回数据
        return Result.ok(map);
    }
}

